package sjjg.graph;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class Digraph {
    private final int V;//节点个数
    private int E;//有向边数
    private Queue<Integer>[]adj;
    Digraph(int V){
           this.V=V;
           this.E=0;
           adj=new LinkedBlockingQueue[V];
        for (int v = 0; v < V; v++) {
            adj[v]=new LinkedBlockingQueue<>();
        }
    }
    public int V(){
        return V;
    }
    public int E(){
        return E;
    }
    public void addEdge(int v,int w){//v->w的边
           adj[v].add(w);
           this.E++;
    }
    public Queue<Integer> adj(int v){
           return adj[v];
    }
    public Digraph reverse(){//得到反向图
        Digraph newDigraph = new Digraph(V);
        for (int v = 0; v < V; v++) {
            for (Integer w : adj[v]) {
                 newDigraph.addEdge(w,v);//w->v
            }
        }
        return newDigraph;
    }
}
